
package com.cnksi.dwz.web.taglib;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;
import org.springside.modules.utils.Reflections;

import com.cnksi.dwz.web.ILinked;

/**
 * DWZ Lookup TREE
 * 
 * DWZ 查找带回 Tree TAG 
 * 
 * <p>
 * 
 * 传入的targets对象必须是实现了ILinked接口的对象
 * 
 * @author dell
 *
 */
public class LookupTreeTAG extends TagSupport
{

	private List<ILinked> targets;

	private String fields;

	private static final long serialVersionUID = 1L;

	public int doEndTag() throws JspException
	{

		try
		{
			pageContext.getOut().write(getHtml());

		} catch (IOException e)
		{
			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

	public String getHtml()
	{

		if (targets != null && targets.size() > 0)
		{
			StringBuffer sb = new StringBuffer();

			sb.append("<ul class='tree expand'>");

			for (ILinked link : targets)
			{
				if (link.getPLink() == null)
				{

					sb.append("<li><a href='javascript:' onclick=\"" + getOnClick(link) + "\">" + link.getLinkname() + "</a>");

					sb.append("<ul>");

					for (ILinked subLink : targets)
					{
						if (subLink.getPLink() != null && subLink.getPLink().getLinkid().equals(link.getLinkid()))
						{

							sb.append("<li><a href='javascript:' onclick=\"" + getOnClick(subLink) + "\">" + subLink.getLinkname() + "</a>");

							getChildren(subLink.getLinkid(), sb);

							sb.append("</li>");
						}
					}

					sb.append("</ul>");

					sb.append("</li>");
				}
			}
			sb.append("</ul>");

			return sb.toString();
		}

		return "";
	}

	/**
	 * 循环判断,获取父节点parentId 下的子节点
	 * 
	 * @param parentId
	 * @param sb
	 * @throws IOException
	 */
	private void getChildren(Object parentId, StringBuffer sb)
	{

		try
		{
			for (ILinked link : targets)
			{
				if (link.getPLink() != null && link.getPLink().getLinkid().equals(parentId))
				{
					sb.append("<ul>");

					sb.append("<li><a href='javascript:' onclick=\"" + getOnClick(link) + "\">" + link.getLinkname() + "</a></li>");

					getChildren(link.getLinkid(), sb);

					sb.append("</ul>");

				}
			}
		} catch (Exception ex)
		{
			ex.printStackTrace();
		}

	}

	private String getOnClick(ILinked linked)
	{

		StringBuilder sbuider = new StringBuilder("$.bringBack({");

		//$.bringBack({menuid:'1', menuname:'权限配置'})
		if (StringUtils.isNotEmpty(fields))
		{

			String[] fieldNames = fields.split(",");

			for (String filedName : fieldNames)
			{
				sbuider.append(filedName + ":\'" + Reflections.getFieldValue(linked, filedName) + "\',");
			}
		}

		sbuider.append("})");

		return sbuider.toString();
	}

	public List<ILinked> getTargets()
	{

		return targets;
	}

	public void setTargets(List<ILinked> targets)
	{

		this.targets = targets;
	}

	public String getFields()
	{

		return fields;
	}

	public void setFields(String fields)
	{

		this.fields = fields;
	}

}
